﻿

using System;
using System.Data;
using Model;
namespace Dal
{
	public class DbOrdersDal
	{
		public DbOrdersDal()
		{
		}
 		#region Insert
 		public static int Insert(DbOrders dbOrders)
		{	
			string sql = @"Insert Into DbOrders (
					OSN,
					OProdName,
					OProdId,
					OProdClass,
					OProdPrices,
					OProdNum,
					OUserId,
					OTotalPrices,
					ONotes,
					OBuyTime,
					OIsDelived,
					OUserName,
					OUserTel,
					OUserAddress
				) values('{0}','{1}',{2},{3},{4},{5},{6},{7},'{8}','{9}',{10},'{11}','{12}','{13}')";
			sql = string.Format(sql,
					SqlHelper.String(dbOrders.OSN),
					SqlHelper.String(dbOrders.OProdName),
					dbOrders.OProdId,
					dbOrders.OProdClass,
					dbOrders.OProdPrices,
					dbOrders.OProdNum,
					dbOrders.OUserId,
					dbOrders.OTotalPrices,
					SqlHelper.String(dbOrders.ONotes),
					dbOrders.OBuyTime,
					dbOrders.OIsDelived,
					SqlHelper.String(dbOrders.OUserName),
					SqlHelper.String(dbOrders.OUserTel),
					SqlHelper.String(dbOrders.OUserAddress)
				);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
		
		#region Update
 		public static int Update(DbOrders dbOrders)
		{	
			string sql = @"Update DbOrders set 

					OSN= '{0}',
					OProdName= '{1}',
					OProdId= {2},
					OProdClass= {3},
					OProdPrices= {4},
					OProdNum= {5},
					OUserId= {6},
					OTotalPrices= {7},
					ONotes= '{8}',
					OBuyTime= '{9}',
					OIsDelived= {10},
					OUserName= '{11}',
					OUserTel= '{12}',
					OUserAddress= '{13}'
				where

					OId= {14}";
			sql = string.Format(sql,
					SqlHelper.String(dbOrders.OSN),
					SqlHelper.String(dbOrders.OProdName),
					dbOrders.OProdId,
					dbOrders.OProdClass,
					dbOrders.OProdPrices,
					dbOrders.OProdNum,
					dbOrders.OUserId,
					dbOrders.OTotalPrices,
					SqlHelper.String(dbOrders.ONotes),
					dbOrders.OBuyTime,
					dbOrders.OIsDelived,
					SqlHelper.String(dbOrders.OUserName),
					SqlHelper.String(dbOrders.OUserTel),
					SqlHelper.String(dbOrders.OUserAddress),
					dbOrders.OId);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
 
 

 		#region Delete
 		public static int Delete(Int32 oId)
		{
		
			string sql = @"Delete from DbOrders where 
					OId= {0}";
			sql = string.Format(sql,
					oId
);
			return SqlHelper.ExecSql(sql);
		} 
		#endregion
 
 		#region Select
 		
 		public static DataSet Select()
		{
			string sql = "select a.* from DbOrders a "; 
			return SqlHelper.Select(sql);
		}
				
		public static DbOrders Select(Int32 oId)
		{
			DbOrders dbOrders = null;
			string sql = @"select a.* from DbOrders a  where
					a.OId= {0}
";
			sql = string.Format(sql,
					oId
);
			DataSet ds = SqlHelper.Select(sql);
			if(SqlHelper.HasRow(ds))
				dbOrders = new DbOrders(ds.Tables[0].Rows[0]);					
			return dbOrders;
		}
		
		public static DataSet Select(string where)
		{ 
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from DbOrders a  where 1=1 " + where ;
			return SqlHelper.Select(sql);
		}
		
		public static DataSet SelectForList(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;
						
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from dbOrders a where 1=1 " + where;
                
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}		
		public static DataSet Select(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;			
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;			
			string sql = "select a.* from DbOrders a  where 1=1 " + where;
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}
		#endregion
	}
}
